package Shared;

import java.awt.Point;
import java.io.Serializable;

/**
 * MapPosition is an object used by the Map class. It represents a position on a
 * road in a map. In the graph (representing the map), the nodes are made of
 * MapPosition instances. Edges between them are road from a position to another
 * position.
 */
public class MapPosition implements Serializable {

	private static final long serialVersionUID = -5587967196526297346L;

	private static int id_static = 0;

	// id of position
	private final int id;

	// x and y coordinate of position
	private final int x, y;

	// status
	private int status; /* on le laisse, au cas o� */

	// Object Point corresponding to the MapPosition
	// private Point point = null;

	/**
	 * Creates a position
	 * 
	 * @param x2
	 *            - x-coordinate
	 * @param y2
	 *            - y-coordinate
	 */
	public MapPosition(int x2, int y2) {
		this.x = x2;
		this.y = y2;
		this.id = id_static++;
	}

	/**
	 * Creates a position (alternative constructor)
	 * 
	 * @param a
	 *            - a point
	 */
	public MapPosition(Point a) {
		this((int) a.getX(), (int) a.getY()); // for interoperability
		// this.point = a;
	}

	/**
	 * Gets the coordinates of the MapPosition
	 * 
	 * @return an 2-element array containing the x and y coordinates of the
	 *         position
	 */
	public int[] getCoordinates() {
		// renvoyer un Point plutot que int[] ?
		int[] t = { this.x, this.y };
		return t;
	}

	// /**
	// * Changes the x and y coordinates of a MapPosition
	// * @param x - new x-coordinate
	// * @param y - new y-coordinate
	// */
	// public void setCoordinates(int x, int y)
	// {
	// this.x = x;
	// this.y = y;
	// }

	/**
	 * 
	 * @return id of position
	 */
	public int getId() {
		return id;
	}

	/**
	 * 
	 * @return true if the position is free( no emergencies, no ambulances,...)
	 */
	public boolean isFree() {
		return (this.status == 0);
	}

	/**
	 * Sets an emergency at this position
	 */
	public void setEmergency() {
		this.status = 2;
	}

	/**
	 * 
	 * @return true if there is an emergency on this position
	 */
	public boolean isEmergency() {
		return (this.status == 2);
	}

	/**
	 * Sets this position to free
	 */
	public void setFree() {
		this.status = 0;
	}

	/**
	 * Sets this position as unreachable
	 */
	public void setUnreachable() {
		this.status = -1;
	}

	/**
	 * 
	 * @return true if this position is reachable
	 */
	public boolean isReachable() {
		return (this.status != -1);
	}

	/**
	 * to string method
	 */
	public String toString() {
		return "<id:" + id + " x:" + x + " y:" + y + " status:" + status + ">";
	}

	/**
	 * 
	 * @return The point corresponding to the MapPosition
	 */
	public Point getPoint() {
		return new Point(this.x, this.y);
	}

	/**
	 * @return True if the two positions are the same
	 */
	public boolean equals(Object other) {
		if (!(other instanceof MapPosition) || other == null)
			return false;

		return (Map.getInstance().getNearestPos((MapPosition) other)).getPoint().equals(
				Map.getInstance().getNearestPos(this).getPoint());

	}

}
